#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <QRegExpValidator>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //LineEdit_1
    //初始化第一个输入框, 用来输入姓名
    ui->lineEdit_name->setPlaceholderText("请输入姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);

    //初始化第二个输入框, 用来输入密码
    ui->lineEdit_password->setPlaceholderText("请输入密码");
    ui->lineEdit_password->setClearButtonEnabled(true);
    ui->lineEdit_password->setEchoMode(QLineEdit::Password);//设置成显示密码的模式

    //初始化第三个输入框, 用来输入电话号码
    ui->lineEdit_name->setPlaceholderText("请输入电话号码");
    ui->lineEdit_name->setClearButtonEnabled(true);
    //电话号码有固定格式
    ui->lineEdit_phone->setInputMask("000-0000-0000");

    //line_Edit2
    QRegExp regExp("^1\\d{10}$");//正则
    ui->lineEdit_phone2->setValidator(new QRegExpValidator(regExp));

    //line_Edit3
    //初始化，把这两个输入框的echoMode完成设置
    ui->lineEdit_password_1->setEchoMode(QLineEdit::Password);
    ui->lineEdit_password_2->setEchoMode(QLineEdit::Password);


    //TextEdit


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_submit_clicked()
{
    QString gender = ui->radioButton_male->isChecked() ? "男" : "女";

        qDebug() << "姓名: " << ui->lineEdit_name->text()
                 << "密码: " << ui->lineEdit_password->text()
                 << "性别: " << gender
                 << "电话: " << ui->lineEdit_phone->text();
}

void Widget::on_lineEdit_phone2_textEdited(const QString &text)
{
    QString content = text;
    int pos = 0;
    if(ui->lineEdit_phone2->validator()->validate(content, pos) == QValidator::Acceptable)
        ui->pushButton_submit2->setEnabled(true);//验证通过
    else
        ui->pushButton_submit2->setEnabled(false);//验证不通过

}

void Widget::on_lineEdit_password_1_textEdited(const QString &arg1)
{
    this->compare();
}

void Widget::on_lineEdit_password_2_textEdited(const QString &arg1)
{
    this->compare();
}

void Widget::compare()
{
    const QString& s1 = ui->lineEdit_password_1->text();
    const QString& s2 = ui->lineEdit_password_2->text();
    if(s1.isEmpty() && s2.isEmpty())
    {
        ui->label_5->setText("密码为空");
    }else if(s1 == s2)
        ui->label_5->setText("两次输入的密码一致");
    else
        ui->label_5->setText("两次输入的密码不一致");
}

void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)//true是显示密码
        ui->lineEdit_password->setEchoMode(QLineEdit::Normal);
    else
        ui->lineEdit_password->setEchoMode(QLineEdit::Password);
}

void Widget::on_textEdit_textChanged()
{
    // 获取到多行输入框中的内容
    const QString& text = ui->textEdit->toPlainText();
    qDebug() << "testChanged: " << text;
    ui->label_2_1->setText(text);
}

void Widget::on_textEdit_selectionChanged()
{
    QTextCursor cursor = ui->textEdit->textCursor();
    qDebug() << "selectionChanged: " << cursor.selectedText();
}



void Widget::on_textEdit_cursorPositionChanged()
{
    QTextCursor cursor = ui->textEdit->textCursor();
    qDebug() << "cursorPositionChanged: " << cursor.position();
}

void Widget::on_textEdit_undoAvailable(bool b)
{
    qDebug() << "undoAvailable: " << b;
}



void Widget::on_textEdit_redoAvailable(bool b)
{
    qDebug() << "redoAvailable: " << b;
}

void Widget::on_textEdit_copyAvailable(bool b)
{
    qDebug() << "copyAvailable: " << b;
}
